
public class ListStack implements Stack {
    private class Node {
	Object item;
	Node next;
    }
    private Node head;

    public ListStack() {
	clear();
    }
    public void clear() {
	head = null;
    }
    public boolean isEmpty() {
	return head == null;
    }
    public void push(Object x) {
	Node n = new Node();
	n.item = x;
	n.next = head;
	head = n;
    }
    public Object pop() {
	if (isEmpty())
	    throw new java.util.EmptyStackException();
	Object x = head.item;
	head = head.next;
	return x;
    }
    public Object top() {
	if (isEmpty())
	    throw new java.util.EmptyStackException();
	return head.item;
    }
}

